﻿using CMS.Util;
using Newtonsoft.Json;
using System;

namespace CMS.Infos
{
    /// <summary>
    /// 学生课程数据对象
    /// </summary>
    public class CourseAssistant : ICloneable, IAssignable
    {
        [JsonProperty(PropertyName = "assistant_id")]
        public int AssistantID { get; set; }

        [JsonProperty(PropertyName = "term_id")]
        public int TermID { get; set; }

        [JsonProperty(PropertyName = "week")]
        public byte Week { get; set; }

        [JsonProperty(PropertyName = "class_week")]
        public uint ClassWeekRaw { get; set; }

        [JsonProperty(PropertyName = "class_time")]
        public uint ClassTimeRaw { get; set; }

        public string ClassWeek { get => Utils.ConvertValue(ClassWeekRaw); set => ClassWeekRaw = Utils.ConvertValue(value); }

        [JsonIgnore]
        public string ClassTime { get => Utils.ConvertValue(ClassTimeRaw); set => ClassTimeRaw = Utils.ConvertValue(value); }

        [JsonProperty(PropertyName = "course")]
        public string Course { get; set; }

        public object Assign(object src)
        {
            if (src is CourseAssistant src2)
            {
                AssistantID = src2.AssistantID;
                TermID = src2.TermID;
                Week = src2.Week;
                ClassWeek = src2.ClassWeek;
                ClassTime = src2.ClassTime;
                Course = src2.Course;
            }
            else
            {
                throw new ArgumentException();
            }
            return this;
        }

        public object Clone()
        {
            return new CourseAssistant().Assign(this);
        }

        public override int GetHashCode()
        {
            return (int)(AssistantID ^ TermID ^ Week ^ ClassWeekRaw ^ ClassTimeRaw ^ (Course?.GetHashCode() ?? 0));
        }

        public override bool Equals(object obj)
        {
            if (obj is CourseAssistant course)
            {
                return AssistantID == course.AssistantID && TermID == course.TermID &&
                    Week == course.Week && ClassWeekRaw == course.ClassWeekRaw &&
                    ClassTimeRaw == course.ClassTimeRaw && Course == course.Course;
            }
            else
            {
                return false;
            }
        }

        public override string ToString()
        {
            return $"{Course}_{AssistantID}";
        }

    }
}
